<?php //$Rev:: 36         $ // DO NOT REMOVE

error_reporting(E_ALL);

/**
 * Utilities class
 * 
 * @author Andrew Heebner <andrew@evilwalrus.com> 
 * @version $Revision: 36 $
 * @require PHP 5.0
 */
class Utilities
{

	public $core;

	/**
	 * __construct: Utilities constructor
	 * 
	 * @param object $object Object to store for parent referencing
	 * @access private 
	 * @return void 
	 */
	public function __construct(ATAC $object)
	{
		$this->core = $object;
	} 

	/**
	 * __destruct: Utilities destructor
	 * 
	 * @access public 
	 * @return void 
	 */
	public function __destruct()
	{
		unset($this->core);
	} 

	/**
	 * __call: Utilities invalid method parser
	 * 
	 * @param string $method Invalid method called
	 * @access private 
	 * @return void 
	 * @throws ATACException
	 */
	public function __call($method, $args)
	{
		throw new ATACException('Invalid method called => (' . __CLASS__ . '::' . $method . ')');
	}

	/**
	 * aim_is_admin: Checks is user is allowed to access restricted commands
	 * 
	 * @param string $nick Screenname of user to check authorization
	 * @access public 
	 * @return boolean 
	 * @proto boolean aim_is_admin(string screenname)
	 */
	public function aim_is_admin($nick)
	{
		if ($this->core->user['admin'] === true) {
			if (is_array($this->core->user['admins'])) {
				if (in_array($this->core->aim_normalize($nick), $this->core->user['admins'])) {
                    return true;
                }
			} elseif($this->core->aim_normalize($nick) == $this->core->user['admins']) {
                return true;
            }
		} 
		return false;
	}

	/**
	 * aim_onlinetime: Display how long TOC has been connected
	 * 
	 * @access public 
	 * @return array 
	 * @proto array aim_onlinetime(void)
	 */
	public function aim_onlinetime()
	{
        $so = Db::getValue('SELECT signon FROM atac_stats');
		$s = time() - $so;
		$d = intval($s / 86400);
		$s -= $d * 86400;
		$h = intval($s / 3600);
		$s -= $h * 3600;
		$m = intval($s / 60);
		$s -= $m * 60;
		return array('days' => $d, 'hours' => $h, 'mins' => $m, 'secs' => $s);
	}

	/**
	 * aim_generate_uuid: Generate a UUID
	 * 
	 * @access public 
	 * @return string 
	 * @proto string aim_generate_uuid(void)
	 */
    public function aim_generate_uuid()
    {
        return strtoupper(sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
            mt_rand(0, 0x0fff) | 0x4000,
            mt_rand(0, 0x3fff) | 0x8000,
            mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)));
    }

	/**
	 * aim_send_im: Send IM to specified user
	 * 
	 * @param string $user User to send IM to
	 * @param string $text Text to send to user
     * @param boolean $enc_off Send encoded IM via TOC2 protocol (off if true)
	 * @param boolean $auto (optional) Whether to send IM as an autoresponder
	 * @access public 
	 * @return void 
	 * @proto void aim_send_im(string user, string text [, boolean autorespond])
	 */
	public function aim_send_im($user, $text, $enc_off = false, $auto = false)
	{
        if ($enc_off === true) {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_send_im %s %s%s', $this->core->aim_normalize($user), $this->core->aim_encode($text), (($auto)?' auto':'')));
        } else {
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc2_send_im_enc %s F U %s %s%s', $this->core->aim_normalize($user), 'en', utf8_encode($this->core->aim_encode($text)), (($auto)?' auto':'')));
        }
	}

	/**
	 * aim_set_away: Sets user status to away or back
	 * 
	 * @param string $message (optional)Away message to send to TOC server
	 * @access public 
	 * @return void 
	 * @proto void aim_set_away([mixed message])
	 */
	public function aim_set_away($message = null)
    {
        if (is_null($message) || empty($message)) {
            $this->core->away['message'] = '';
            $this->core->away['isaway'] = false;
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc_set_away');
            $this->aim_set_idle(0);
            $this->core->aim_debug('You are no longer away/idle');
        } else {
            $this->core->away['message'] = trim($message);
            $this->core->away['isaway'] = true;
            $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_set_away %s', $this->core->aim_encode($this->core->away['message'])));
            $this->aim_set_idle(1);
            $this->core->aim_debug('You are now marked as away/idle');
        }
	} 

	/**
	 * aim_warn: Warn a user
	 * 
	 * @param string $user User to warn
	 * @param boolean $anonymous (optional) Whether to warn anonymously
	 * @access public 
	 * @return void 
	 * @proto void aim_warn(string user [, boolean anonymous_warn])
	 */
	public function aim_warn($user, $anonymous = false)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_evil %s %s', $this->core->aim_normalize($user), ($anonymous) ? 'anon': 'norm'));
	} 

	/**
	 * aim_set_info: Set info for user (simple HTML)
	 * 
	 * @param string $info Simple HTML userinfo
	 * @access public 
	 * @return void 
	 * @proto void aim_set_info(string info)
	 */
	public function aim_set_info($info)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc_set_info ' . $this->core->aim_encode($info));
	} 

	/**
	 * aim_get_info: Get info for the specified user
	 * 
	 * @param string $user User to get info for
	 * @access public 
	 * @return void 
	 * @proto N/A
	 */
	public function aim_get_info($user)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc_get_info ' . $this->core->aim_normalize($user));
	} 

	/**
	 * aim_set_idle: Sets a user to idle
	 * 
	 * @param integer $seconds (optional) Time (in seconds) to idle user for
	 * @access public 
	 * @return void 
	 * @proto void aim_set_idle([integer seconds])
	 */
	public function aim_set_idle($seconds = 1)
	{
		$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, 'toc_set_idle ' . $seconds);
	} 

	/**
	 * aim_format_sn: Apply special formatting to screenname
	 * 
	 * @param string $new Newly formatted screenname
	 * @access public 
	 * @return boolean 
	 * @proto boolean aim_format_sn(string new_screenname)
	 */
	public function aim_format_sn($new)
	{
		$new = trim($new);
		if ((strlen($new) > 16) || $this->core->aim_normalize($this->core->user['screenname']) != $this->core->aim_normalize($new)) {
			$this->core->aim_debug('Newly formatted screenname > 16 chars, or does not match old screenname', AIM_WARN);
            return false;
		} else {
			$this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc_format_nickname %s', $this->core->aim_encode($new)));
            $this->core->aim_debug('Screenname successfully formatted', AIM_INFO);
            return true;
		}
        return false;
	}

	/**
	 * aim_client_event: Send a typing event to a user
	 * 
	 * @param string $user User to send event to
     * @param integer $status Status code to send
	 * @access public 
	 * @return void 
	 * @proto boolean aim_client_event(string user, integer status)
	 */
    public function aim_client_event($user, $status)
    {
        $this->core->sflap->aim_sflap_send(SFLAP_TYPE_DATA, sprintf('toc2_client_event "%s" %s', $this->core->aim_normalize($user), $status));
    }

} // End Utilities

?>